import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        name: 'home',
        component: HomeView
    },
    {
        path: '/report',
        name: 'report',
        component: () => import(/* webpackChunkName: "about" */ '../views/ReportPlan.vue'),
        // meta: { requiresAuth: true }
    },
    {
        path: '/plan',
        name: 'plan',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "about" */ '../views/PlanView.vue'),
        // meta: {requiresAuth: true}
    },
]

const router = new VueRouter({
    routes
})
router.beforeEach((to, from, next) => {
    const valueFromSessionStorage = sessionStorage.getItem('login'); // 从 sessionStorage 中读取数据
    console.log(to)
    // if (to.name === 'report' || to.name === 'plan') {
    //     if (valueFromSessionStorage === 'true') {
    //         next();
    //     } else {
    //         next('/');
    //     }
    // } else {
    //     next();
    // }
    next();
    console.log("登录凭证:", valueFromSessionStorage)
})
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
    return originalPush.call(this, location).catch(err => err)
}

export default router
